<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="套餐名称" prop="type">
        <el-input v-model="dataForm.type" placeholder="套餐名称"></el-input>
      </el-form-item>
      <el-form-item label="套餐价格" prop="price">
        <el-input v-model="dataForm.price" placeholder="套餐价格" type="number"></el-input>
      </el-form-item>
        <el-form-item label="排序" prop="sort">
        <el-input-number v-model="dataForm.sort" placeholder="排序(越大越靠前)" ></el-input-number>
      </el-form-item>
      <el-form-item label="产品" prop="productId">
        
        <el-input v-model="dataForm.miniProductName" placeholder="产品ID" :disabled="true"></el-input>
        <!-- <el-select v-model="dataForm.productId" placeholder="请选择">
          <el-option
            v-for="miniProduct in miniProductOptions"
            :key="miniProduct.id"
            :label="miniProduct.name"
            :value="miniProduct.id">
          </el-option>
        </el-select> -->
      </el-form-item>
      <el-form-item label="时间:天" prop="day">
        <el-input v-model="dataForm.day" placeholder="天" type="number"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  export default {
    data () {
      return {
        visible: false,
        miniProductOptions: [],
        dataForm: {
          id: 0,
          price: '',
          productId: 0,
          year: 0,
          type: '',
          sort:0,
          miniProductName: ''
        },
        dataRule: {
          price: [
            { required: true, message: '产品价格不能为空', trigger: 'blur' }
          ],
          productId: [
            { required: true, message: '产品ID不能为空', trigger: 'blur' }
          ],
          year: [
            { required: true, message: '时间不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
           sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
    init (id,productId) {
      this.dataForm.id = id
      this.visible = true
      this.miniProductOptions = []
      this.dataForm.productId = 0
      this.dataForm.miniProductName = ''
      this.$nextTick(() => {
      this.$refs['dataForm'].resetFields()
      if (this.dataForm.id) {
        API.miniproducprice.info(this.dataForm.id).then(({data}) => {

          if (data && data.code === 0) {
            this.dataForm.price = data.miniProductPrice.price
            this.dataForm.productId = data.miniProductPrice.productId
            this.dataForm.day = data.miniProductPrice.day
            this.dataForm.type = data.miniProductPrice.type
            this.dataForm.sort = data.miniProductPrice.sort
            this.dataForm.miniProductName = data.miniProductPrice.productName
          }
        }) 
      } else {
        this.miniProductOptions = []
        var params = {
          pageIndex: 1,
          pageSize: 100
        }
        API.miniproduct.info(productId).then(({data}) => {
          this.dataForm.productId = productId
          this.dataForm.miniProductName = data.miniProduct.name
        })
      }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          var params = {
            'id': this.dataForm.id,
            'price': this.dataForm.price || undefined,
            'productId': this.dataForm.productId || undefined,
            'day': this.dataForm.day || undefined,
            'sort': this.dataForm.sort || undefined,
            'type': this.dataForm.type || undefined,
          }
          var tick = !this.dataForm.id ? API.miniproducprice.add(params) : API.miniproducprice.update(params)
          tick.then(({data}) => {
            if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
        })
      }
    }
  }
</script>
